package com.qf.sy2103.ssm.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.sy2103.ssm.constants.Constants;
import com.qf.sy2103.ssm.dao.UserDao;
import com.qf.sy2103.ssm.entity.User;
import com.qf.sy2103.ssm.service.UserServcie;
import com.qf.sy2103.ssm.vo.PageVo;
import com.qf.sy2103.ssm.vo.TableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserServcie {
    @Autowired
    private UserDao userDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int regist(User user) {
        int insert = userDao.insert(user);
        return insert;
    }
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User login(String name, String password) {
        User user = userDao.selectnamepassword(name, password);
        return user;
    }
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User sname(String name) {
        User u = userDao.selectname(name);
        return u;
    }
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public TableVo<User> userAll(PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        List<User> users = userDao.userAll();
        PageInfo<User> pageInfo=new PageInfo<>(users);
        return new TableVo<User>(0,"ok",pageInfo.getTotal(),users);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int edit(User user) {
        return userDao.updata(user);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int del(Integer[] id) {
        return userDao.delete(id);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public TableVo<User> cha(PageVo pageVo,User user) {
        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        List<User> users = userDao.selectcha(user);
        PageInfo<User> pageInfo=new PageInfo<>(users);
        return new TableVo<User>(0,"ok",pageInfo.getTotal(),users);
    }
//    @Transactional(propagation = Propagation.SUPPORTS)
//    @Override
//    public String exportAllData(String realPath) throws IOException {
//
//        // 1. 调用 userDao 查出所有的用户数据
//        final List<User> allUsers = userDao.userAll();
//
//        // 2. 把数据写入文件
//        File file = new File(realPath);
//        if (!file.exists()) {
//            file.mkdirs();
//        }
//        //定制全局唯一的命名
//        String unique = UUID.randomUUID().toString()+ ".csv";
//
//        final String finalFileName = realPath + File.separator + unique ;
//
//
//        writeListToFile(finalFileName, allUsers);
//
//        return unique;
//    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public String exportAllDataBatch(String realPath) throws IOException {

        // 2. 把数据写入文件
        File file = new File(realPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        //定制全局唯一的命名
        String unique = UUID.randomUUID().toString()+ ".csv";

        final String finalFileName = realPath + File.separator + unique ;


        // 1. 分批导出，到底分多少批呢？
        Integer pageSize = 10;

        Integer count = userDao.count();

        Integer times = 0; // 需要循环多少次

        if( count%pageSize == 0 ){  // 整除时
            times = count / pageSize;
        }else{
            times = count / pageSize + 1;
        }
        for (int i = 0; i < times; i++) {

            PageHelper.startPage((i+1),pageSize);
            final List<User> allUsers = userDao.userAll();

            writeListToFile(finalFileName, allUsers);
        }


        return unique;
    }

    private void writeListToFile(String finalFileName, List<User> allUsers) throws  IOException {
        final BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new File(finalFileName), true));

        for (User user : allUsers) {
            String s = user.toCSVString();
            bufferedWriter.write(s);
            bufferedWriter.newLine();
        }

        bufferedWriter.flush();
        bufferedWriter.close();
    }
}
